package com.auto.video.tv.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.auto.video.tv.R;

public class WeatherItemView extends LinearLayout {
    private static final String TAG = "WeatherItemView";
    private EditText mValueView;

    public WeatherItemView(Context context) {
        this(context, null);
    }

    public WeatherItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeatherItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public WeatherItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.app_weather_adapter_item, this);
        LinearLayout titleBg = view.findViewById(R.id.tag_background);
        TextView titleView = view.findViewById(R.id.weather_title);
        TextView subTitleView = view.findViewById(R.id.sub_title);
        mValueView = view.findViewById(R.id.weather_value);
        TextView unitView = view.findViewById(R.id.weather_unit);

        @SuppressLint("CustomViewStyleable")
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TemptureItemView);
        Drawable backgroundBg = typedArray.getDrawable(R.styleable.TemptureItemView_backgroundBg);
        titleBg.setBackground(backgroundBg);
        titleView.setText(typedArray.getText(R.styleable.TemptureItemView_title));
        subTitleView.setText(typedArray.getText(R.styleable.TemptureItemView_subTitle));
        mValueView.setText(typedArray.getString(R.styleable.TemptureItemView_valueText));
        unitView.setText(typedArray.getText(R.styleable.TemptureItemView_unit));

        float subTitleSize = typedArray.getFloat(R.styleable.TemptureItemView_subTitleTextSize, 18);
        subTitleView.setTextSize(subTitleSize);
        float valueSize = typedArray.getFloat(R.styleable.TemptureItemView_textTextSize, 20);
        mValueView.setTextSize(valueSize);
        float unitSize = typedArray.getFloat(R.styleable.TemptureItemView_unitTextSize, 22);
        unitView.setTextSize(unitSize);

        typedArray.recycle();
    }

    public void setValueText(String value) {
        mValueView.setText(value);
    }

    public String getText() {
        return  mValueView.getText().toString();
    }

    /**
     * 设置可编辑模式
     *
     * @param isEditable 是否可编辑
     */
    public void setEditable(boolean isEditable) {
        mValueView.setFocusable(isEditable);
        mValueView.setFocusableInTouchMode(isEditable);
    }

    public void requestViewFocus() {
        mValueView.requestFocus();
    }
}
